#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
创建DMG背景图的工具脚本
基于用户提供的PNG图标样式创建DMG背景
"""

from PIL import Image, ImageDraw, ImageFont
import os
from pathlib import Path

def create_dmg_background(width=640, height=480, output_path="resources/dmg_background.png"):
    """
    创建DMG背景图

    Args:
        width: 背景图宽度
        height: 背景图高度
        output_path: 输出路径
    """
    # 创建输出目录
    output_file = Path(output_path)
    output_file.parent.mkdir(exist_ok=True)

    # 创建渐变背景
    img = Image.new('RGB', (width, height), (245, 245, 245))
    draw = ImageDraw.Draw(img)

    # 创建浅色渐变效果
    for y in range(height):
        # 从浅灰到白色的渐变
        color_val = int(245 + (255 - 245) * (y / height))
        color = (color_val, color_val, color_val)
        draw.line([(0, y), (width, y)], fill=color)

    # 添加淡淡的网格效果
    grid_size = 40
    grid_color = (250, 250, 250)

    # 垂直线
    for x in range(0, width, grid_size):
        draw.line([(x, 0), (x, height)], fill=grid_color, width=1)

    # 水平线
    for y in range(0, height, grid_size):
        draw.line([(0, y), (width, y)], fill=grid_color, width=1)

    # 在底部添加淡淡的品牌标识区域
    footer_height = 60
    footer_y = height - footer_height

    # 底部渐变区域
    for y in range(footer_y, height):
        alpha = (y - footer_y) / footer_height
        color_val = int(245 - 20 * alpha)  # 稍微深一点
        color = (color_val, color_val, color_val)
        draw.line([(0, y), (width, y)], fill=color)

    # 保存图片
    img.save(output_file, 'PNG', quality=95)
    print(f"DMG背景图已创建: {output_file}")
    print(f"尺寸: {width}x{height}")

    return str(output_file)

def create_professional_background(width=640, height=480, output_path="resources/dmg_background.png"):
    """
    创建专业的DMG背景图（类似于你提供的PNG样式）
    """
    output_file = Path(output_path)
    output_file.parent.mkdir(exist_ok=True)

    # 创建纯净的浅色背景
    img = Image.new('RGBA', (width, height), (248, 248, 248, 255))
    draw = ImageDraw.Draw(img)

    # 添加微妙的阴影效果（类似文件图标的效果）
    shadow_offset = 2
    shadow_color = (200, 200, 200, 50)

    # 在右下角创建一个大的装饰性文档图标轮廓
    icon_size = 120
    icon_x = width - icon_size - 40
    icon_y = height - icon_size - 40

    # 绘制文档轮廓（类似PNG图标）
    # 主体矩形
    rect_points = [
        (icon_x, icon_y),
        (icon_x + icon_size - 20, icon_y),
        (icon_x + icon_size, icon_y + 20),
        (icon_x + icon_size, icon_y + icon_size),
        (icon_x, icon_y + icon_size)
    ]

    # 绘制阴影
    shadow_points = [(x + shadow_offset, y + shadow_offset) for x, y in rect_points]
    draw.polygon(shadow_points, fill=(0, 0, 0, 20))

    # 绘制主体
    draw.polygon(rect_points, fill=(255, 255, 255, 180), outline=(220, 220, 220, 100))

    # 绘制折角
    fold_points = [
        (icon_x + icon_size - 20, icon_y),
        (icon_x + icon_size, icon_y + 20),
        (icon_x + icon_size - 20, icon_y + 20)
    ]
    draw.polygon(fold_points, fill=(240, 240, 240, 150))

    # 在左上角添加应用程序放置提示区域
    app_area_size = 100
    app_x = 50
    app_y = 80

    # 绘制淡淡的圆角矩形
    app_rect = [app_x, app_y, app_x + app_area_size, app_y + app_area_size]
    draw.rounded_rectangle(app_rect, radius=10, fill=None, outline=(200, 200, 200, 100), width=2)

    # Applications文件夹区域
    apps_x = width - 150
    apps_y = 80
    apps_rect = [apps_x, apps_y, apps_x + app_area_size, apps_y + app_area_size]
    draw.rounded_rectangle(apps_rect, radius=10, fill=None, outline=(200, 200, 200, 100), width=2)

    # 保存图片
    img.save(output_file, 'PNG')
    print(f"专业DMG背景图已创建: {output_file}")
    print(f"尺寸: {width}x{height}")

    return str(output_file)

if __name__ == "__main__":
    # 创建两种样式的背景图供选择

    # 1. 简单渐变背景
    create_dmg_background(640, 480, "resources/dmg_background_simple.png")

    # 2. 专业文档风格背景（推荐，类似用户提供的PNG）
    create_professional_background(640, 480, "resources/dmg_background.png")

    print("\n背景图创建完成！")
    print("- dmg_background.png: 专业文档风格（推荐）")
    print("- dmg_background_simple.png: 简单渐变风格")